package com.mhwx.mhlive.ui.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.mhwx.mhlive.R;
import com.mhwx.mhlive.ui.activity.LiveDetails;
import com.mhwx.mhlive.ui.adapter.HotAdapter;
import com.mhwx.mhlive.ui.adapter.HotNormalAdapter;
import com.mhwx.mhlive.ui.base.BaseFragment;
import com.mhwx.mhlive.util.IntoActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;


/**
 * 类功能描述
 *
 * @author Guol
 * @version 1.0
 * @copyright 北京明华维新信息技术有限公司
 * @time 2017/4/27
 */
public class HotFragment  extends BaseFragment {
    @BindView(R.id.recycler)
    RecyclerView recycler;

    @BindView(R.id.rotate_header_grid_view_frame)
    PtrClassicFrameLayout mPtrFrame;

    RollPagerView mRollViewPager;
    private List<String> mDatas;
    private HotAdapter mAdapter;
    private View headView;

    public static String TABLAYOUT_FRAGMENT = "tab_fragment";
    private int type;

    @Override
    protected int getLayoutRes() {
        return R.layout.frm_hot;
    }

    public static HotFragment newInstance(int type) {
        HotFragment fragment = new HotFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(TABLAYOUT_FRAGMENT, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        init();
    }

    private void init(){
        mDatas = new ArrayList<String>();

        recycler.setLayoutManager(new LinearLayoutManager(getContext()));
        mPtrFrame.setLastUpdateTimeRelateObject(this);
        mPtrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                mPtrFrame.refreshComplete();
                initData();
            }

        });

    }
    protected void initData()
    {

        for (int i = 'A'; i < 'z'; i++)
        {
            mDatas.add("" + (char) i);
        }
        if (mAdapter==null) mAdapter=new HotAdapter(getContext(),mDatas);
        recycler.setAdapter(mAdapter);
        if (headView==null) {
            mAdapter.addHeaderView(getViewId());
        }
        mAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {
                IntoActivity.toNextActivity(getContext(), LiveDetails.class);
            }
        });

    }
    private View getViewId(){
         headView = LayoutInflater.from(getActivity()).inflate(R.layout.item_hot_hear,null , false);
        mRollViewPager= (RollPagerView) headView.findViewById(R.id.roll_view_pager);
        //设置播放时间间隔
        mRollViewPager.setPlayDelay(1000);
        //设置透明度
        mRollViewPager.setAnimationDurtion(500);
        //设置适配器
        mRollViewPager.setAdapter(new HotNormalAdapter());
        // 设置圆点指示器颜色
        mRollViewPager.setHintView(new ColorPointHintView(getContext(), Color.WHITE, Color.GRAY));
        return headView;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = (int) getArguments().getSerializable(TABLAYOUT_FRAGMENT);
        }
    }

}
